package com.xy.usermanagement.accountmanagement.controller;

import com.xy.model.PageList;
import com.xy.model.RestfulResult;
import com.xy.model.UserStatus;
import com.xy.usermanagement.accountmanagement.service.IAccountManagementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("accountmanagement")
public class AccountManagementController {
		@Autowired
		private IAccountManagementService accountManagementService;

		@PutMapping(value = "/ban/", produces = "application/json;charset=UTF-8")
		public RestfulResult banAccount(@RequestBody UserStatus user) {
				log.info("AccountManagementController banAccount=> user : " + user);
				if (user == null) {
						if (user.getDeadlines() == null) {
								return RestfulResult.failed("-300", "截止日期不能为空！");
						}
				}
				Integer rest = accountManagementService.changeAccount(user);
				if (rest == -1) {
						return RestfulResult.failed("-100", "系统繁忙，封号失败！");
				}
				return RestfulResult.success();
		}

		@PutMapping(value = "/unlock/", produces = "application/json;charset=UTF-8")
		public RestfulResult unlockAccount(@RequestBody UserStatus user) {
				if (user.getNickName()== null) {
						return RestfulResult.failed("-300", "截止日期不能为空！");
				}
				Integer rest = accountManagementService.changeAccount(user);
				if (rest == -1) {
						return RestfulResult.failed("-100", "系统繁忙，封号失败！");
				}
				return RestfulResult.success();
		}

		@GetMapping(value = "/")
		public RestfulResult list(Integer pageNum, Integer pageSize, String type) {
				PageList<UserStatus> list = null;
				if (type.equals("ban")) {
						list = accountManagementService.getBanAccount(pageNum, pageSize);
				} else if (type.equals("unlock")) {
						list = accountManagementService.getUnlockAccount(pageNum, pageSize);
				} else {
						list = accountManagementService.getAllAccount(pageNum, pageSize);
				}
				return RestfulResult.success(list);
		}
}
